﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using HYElectricCommon;
using Microsoft.Office.Interop.Word;

namespace YArchitech.Revit.MEP.Calculator
{
	internal class WriteWord
	{
		public void Run()
		{
			Application application = (Application)Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("000209FF-0000-0000-C000-000000000046")));
			object value = Missing.Value;
			object obj = AssistFunc.GetAppRootPath() + "\\Sample\\照度计算书.dot";
			_Document document = application.Documents.Add(ref obj, ref value, ref value, ref value);
			application.Visible = true;
			document.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
			object obj2 = "标题 1";
			application.Selection.TypeText("照度计算计算书");
			application.Selection.ParagraphFormat.set_Style(ref obj2);
			object obj3 = "标题 2";
			application.Selection.TypeParagraph();
			application.Selection.TypeText("计算依据");
			application.Selection.ParagraphFormat.set_Style(ref obj3);
			application.Selection.TypeParagraph();
			application.Selection.TypeText("   灯具数量,照度值计算根据《照明设计手册》第二版(中国电力出版社) P221页公式(5-39)计算。\n   利用系数根据《照明设计手册》第二版(中国电力出版社)表4-1至4-62查询获得。");
			application.Selection.TypeParagraph();
			application.Selection.TypeText("计算结果");
			application.Selection.ParagraphFormat.set_Style(ref obj3);
			application.Selection.TypeParagraph();
			for (int i = 0; i < this.IllCalDataSet.Count; i++)
			{
				IllCalData data = this.IllCalDataSet[i];
				this.WriteRoomInfo(application, data, i + 1);
				this.WriteLampInfo(application, data, i + 1);
				this.WriteLightSrcInfo(application, data, i + 1);
				this.WriteIllRequest(application, data, i + 1);
				this.WriteCalProcess(application, data, i + 1);
				this.WriteCheckResult(application, data, i + 1);
			}
		}

		private bool WriteRoomInfo(Application wordApp, IllCalData data, int index)
		{
			object obj = "标题 3";
			wordApp.Selection.TypeText("房间编号:" + data.roomData.RoomNumber.ToString() + " 房间名称:" + data.roomData.RoomName);
			wordApp.Selection.ParagraphFormat.set_Style(ref obj);
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText(index.ToString() + ".1 房间条件");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    房间面积:" + data.roomData.RoomArea.ToString("0.00") + "平方米");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    房间高度:" + data.roomData.RoomHeight.ToString("0.00") + "米");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    工作面高度:" + data.roomData.WorkHeight.ToString("0.00") + "米");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    安装高度:" + data.roomData.InstallHeight.ToString("0.00") + "米");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    顶棚反射系数:" + data.roomData.CeilingFactor.ToString("0") + "%");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    墙面上部反射系数:" + data.roomData.WallUpFactor.ToString("0") + "%");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    墙面下部反射系数:" + data.roomData.WallDownFactor.ToString("0") + "%");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    地面反射系数:" + data.roomData.FloorFactor.ToString("0") + "%");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    窗户面积:" + data.roomData.WindowArea.ToString("0.00"));
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    窗反射系数:" + data.roomData.WindowFactor.ToString("0") + "%");
			wordApp.Selection.TypeParagraph();
			return true;
		}

		private bool WriteLampInfo(Application wordApp, IllCalData data, int index)
		{
			wordApp.Selection.TypeText(index.ToString() + ".2 灯具信息:");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    灯具类型:" + data.lampData.Type);
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    灯具名称:" + data.lampData.Name);
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    灯具规格:" + data.lampData.Spec);
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    灯具维护系数:" + data.lampData.MaintenanceFactor.ToString("0.00"));
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    利用系数:" + data.lampData.UseFactor.ToString("0.00"));
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    镇流器功率:" + data.lampData.BallastPower.ToString("0.0") + "(W)");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeParagraph();
			return true;
		}

		private bool WriteLightSrcInfo(Application wordApp, IllCalData data, int index)
		{
			wordApp.Selection.TypeText("    灯具使用光源:");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    光源类型:" + data.lightSourceData.Type);
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    光源名称:" + data.lightSourceData.Name);
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    光源型号:" + data.lightSourceData.Model);
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    光源数量:" + data.lightSourceData.Number.ToString());
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    光源功率:" + data.lightSourceData.Power.ToString("0"));
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    光通量:" + data.lightSourceData.LuminousFlux.ToString("0"));
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeParagraph();
			return true;
		}

		private bool WriteIllRequest(Application wordApp, IllCalData data, int index)
		{
			wordApp.Selection.TypeText(index.ToString() + ".3 照度要求:");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    照度要求:" + data.roomData.Lux.ToString("0"));
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    功率密度要求:" + data.roomData.Lpd.ToString("0"));
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeParagraph();
			return true;
		}

		private bool WriteCalProcess(Application wordApp, IllCalData data, int index)
		{
			wordApp.Selection.TypeText(index.ToString() + ".4 计算过程:");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    由 Eav = N×φ×U×K / A");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    得 N = (Eav×A) / (φ×U×K)");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    计算Eav");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    N = (Eav×A) / (φ×U×K)");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText(string.Concat(new string[]
			{
				"       = (",
				data.roomData.Lux.ToString("0"),
				"×",
				data.roomData.RoomArea.ToString("0.00"),
				") / (",
				data.lightSourceData.LuminousFlux.ToString("0"),
				"×",
				data.lightSourceData.Number.ToString(),
				"×",
				data.lampData.UseFactor.ToString("0.00"),
				"×",
				data.lampData.MaintenanceFactor.ToString("0.00"),
				") = ",
				data.calResultData.LampNumber.ToString(),
				"套"
			}));
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeParagraph();
			return true;
		}

		private bool WriteCheckResult(Application wordApp, IllCalData data, int index)
		{
			wordApp.Selection.TypeText(index.ToString() + ".5 校核结果:");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    采用本灯具:" + data.calResultData.LampNumber.ToString() + "套");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    实际照度 E = (N×φ×U×K) / A");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText(string.Concat(new string[]
			{
				"               = (",
				data.calResultData.LampNumber.ToString(),
				"×",
				data.lightSourceData.LuminousFlux.ToString("0"),
				"×",
				data.lightSourceData.Number.ToString(),
				"×",
				data.lampData.UseFactor.ToString("0.00"),
				"×",
				data.lampData.MaintenanceFactor.ToString("0.00"),
				") / ",
				data.roomData.RoomArea.ToString("0.00"),
				" = ",
				data.calResultData.Lux.ToString("0.00"),
				"(LX)"
			}));
			wordApp.Selection.TypeParagraph();
			if (data.calResultData.Lux < data.roomData.Lux * 0.9)
			{
				wordApp.Selection.TypeText("    ∵E < 0.9Eav, ∴计算照度未达到平均照度要求");
				wordApp.Selection.TypeParagraph();
			}
			else if (data.calResultData.Lux > data.roomData.Lux * 1.1)
			{
				wordApp.Selection.TypeText("    ∵E > 1.1Eav, ∴计算照度未达到平均照度要求");
				wordApp.Selection.TypeParagraph();
			}
			else
			{
				wordApp.Selection.TypeText("    ∵0.9Eav< E < 1.1Eav, ∴计算照度达到平均照度要求");
				wordApp.Selection.TypeParagraph();
			}
			wordApp.Selection.TypeText("    计算功率密度：W = 灯具数×(单个灯的镇流器功率＋单个灯的光源容量) / 面积");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText(string.Concat(new string[]
			{
				"                    = ",
				data.calResultData.LampNumber.ToString(),
				"×(",
				data.lampData.BallastPower.ToString("0"),
				" + ",
				data.lightSourceData.Number.ToString(),
				"×",
				data.lightSourceData.Power.ToString("0.0"),
				") / ",
				data.roomData.RoomArea.ToString("0.00"),
				" = ",
				data.calResultData.Lpd.ToString("0.00")
			}));
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    要求的功率密度 X = " + data.roomData.Lpd.ToString("0"));
			wordApp.Selection.TypeParagraph();
			if (data.calResultData.Lux <= 0.0001)
			{
				wordApp.Selection.TypeText("，《建筑照明设计标准》（GB 50034-2004）中无此建筑物的功率密度标准值");
				wordApp.Selection.TypeParagraph();
			}
			wordApp.Selection.TypeText("    换算到照度标准值的功率密度W(换算) = " + data.calResultData.LpdEx.ToString("0.00"));
			wordApp.Selection.TypeParagraph();
			if (data.roomData.Lpd < 0.0001)
			{
				wordApp.Selection.TypeText("    满足功率密度要求。");
				wordApp.Selection.TypeParagraph();
			}
			else if (data.calResultData.LpdEx > 0.0001 && data.calResultData.LpdEx < data.roomData.Lpd)
			{
				wordApp.Selection.TypeText("    W(换算)  < X 满足功率密度要求。");
				wordApp.Selection.TypeParagraph();
			}
			else
			{
				wordApp.Selection.TypeText("    W(换算)  > X 不满足功率密度要求。");
				wordApp.Selection.TypeParagraph();
			}
			return true;
		}

		public List<IllCalData> IllCalDataSet;
	}
}
